package com.yami.shop.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.Product;
import com.yami.shop.bean.model.User;
import com.yami.shop.bean.pt.YxCtjl;
import com.yami.shop.bean.pt.YxPtgl;
import com.yami.shop.bean.pt.YxPtsp;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.*;
import jodd.util.CollectionUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/yxPtgl")
public class YxPtglController {
    @Autowired
    private IYxPtglService yxPtglService;
    @Autowired
    private UserService userService;
    @Autowired
    private ProductService productService;
    @Autowired
    private IYxPtspService yxPtspService;
    @Autowired
    private IYxCtjlService yxCtjlService;




    /**
     * 查询拼团列表
     */
    @PostMapping("/getPtList")
    public ResponseEntity<IPage<YxPtgl>> getPtList(PageParam<YxPtgl> page,YxPtgl yxPtgl){
        IPage<YxPtgl> yxPtglIPage = yxPtglService.selectYxPtglList(page,yxPtgl);
        return ResponseEntity.ok(yxPtglIPage);
    }

    /**
     * 新增保存拼团
     */
    @PostMapping("/save")
    public  ResponseEntity<Void> save(YxPtgl yxPtgl){
        yxPtglService.insertYxPtgl(yxPtgl);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改拼团
     */
    @PostMapping("/updatePt")
    public ResponseEntity<Void> updatePt(YxPtgl yxPtgl){
          yxPtglService.updateYxPtgl(yxPtgl);
          return ResponseEntity.ok().build();
    }

    /**
     * 删除拼团
     */
    @PostMapping("/deletePt")
    public ResponseEntity<Void> deletePt(String  ids){
        yxPtglService.deleteYxPtglByIds(ids);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询已经完成拼团的记录
     */
    @GetMapping("/getPtSuccessedList")
    public ResponseEntity<List<YxPtgl>> getPtSuccessedList(){
        List<YxPtgl> yxPtglList = yxPtglService.getPtSuccessedList();
        if(CollectionUtils.isNotEmpty(yxPtglList)){
            for(YxPtgl yxPtgl:yxPtglList){
                String userId = yxPtgl.getPzid();
                User user = userService.getUserByUserId(userId);
                if(user!=null){
                    yxPtgl.setNickName(user.getNickName());
                    yxPtgl.setPic(user.getPic());
                    yxPtgl.setPhoneNumber(user.getUserMobile());
                }
            }
        }
        return ResponseEntity.ok(yxPtglList);
    }

    /**
     * 查询拼团信息
     */
    @GetMapping("/getPtStatus")
    public ResponseEntity<Map> getPtStatus(Long ptglid){
        HashMap map = new HashMap();
        //查询拼团状态
        YxPtgl yxPtgl =yxPtglService.selectYxPtglById(ptglid);
        if(yxPtgl!=null){
            Long prodId = yxPtgl.getGoodsid();
            Product product = productService.getProductByProdId(prodId);
            YxPtsp yxPtsp = yxPtspService.getYxPtspbyProdId(prodId);
            if(yxPtgl.getStatus().equals("1")){
                map.put("ptStatus",yxPtgl.getStatus());
                if(product!=null){
                    map.put("prodName",product.getProdName());
                    map.put("prodPic",product.getPic());
                    map.put("price",yxPtsp.getPtj());
                }
                //查询拼团用户
                List<User> userList = new ArrayList<User>();
               List<YxCtjl> yxCtjlList = yxCtjlService.selectYxCtjlListPtglid(ptglid);
               if(CollectionUtils.isNotEmpty(yxCtjlList)){
                   for(YxCtjl yxCtjl:yxCtjlList){
                       String userId = yxCtjl.getUserId();
                       User user = userService.getUserByUserId(userId);
                       userList.add(user);
                   }
               }
               map.put("userList",userList);
            }else if(yxPtgl.getStatus().equals("2")){
                map.put("ptStatus",yxPtgl.getStatus());
                if(product!=null){
                    map.put("prodName",product.getProdName());
                    map.put("prodPic",product.getPic());
                    map.put("price",yxPtsp.getPtj());
                }
                //查询拼团用户
                List<User> userList = new ArrayList<User>();
                List<YxCtjl> yxCtjlList = yxCtjlService.selectYxCtjlListPtglid(ptglid);
                if(CollectionUtils.isNotEmpty(yxCtjlList)){
                    for(YxCtjl yxCtjl:yxCtjlList){
                        String userId = yxCtjl.getUserId();
                        User user = userService.getUserByUserId(userId);
                        userList.add(user);
                    }
                }
                map.put("userList",userList);
            }else if(yxPtgl.getStatus().equals("0")){
                map.put("ptStatus",yxPtgl.getStatus());
                map.put("peopleNumber",yxPtgl.getCtrs()-yxPtgl.getPtrs());
                if(product!=null){
                    map.put("prodName",product.getProdName());
                    map.put("prodPic",product.getPic());
                    map.put("price",yxPtsp.getPtj());
                }
                //查询拼团用户
                List<User> userList = new ArrayList<User>();
                List<YxCtjl> yxCtjlList = yxCtjlService.selectYxCtjlListPtglid(ptglid);
                if(CollectionUtils.isNotEmpty(yxCtjlList)){
                    for(YxCtjl yxCtjl:yxCtjlList){
                        String userId = yxCtjl.getUserId();
                        User user = userService.getUserByUserId(userId);
                        userList.add(user);
                    }
                }
                map.put("userList",userList);
            }
        }
        return ResponseEntity.ok(map);
    }


}
